<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="160px"
    >
      <el-form-item label="项目名称" prop="projectId">
        <el-select
          style="width:100%"
          v-model="dataForm.projectId"
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in optionsprojectId"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备序列号" prop="deviceSn">
        <el-input
          v-model="dataForm.deviceSn"
          placeholder="设备序列号"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备IP" prop="deviceAddress">
        <el-input
          v-model="dataForm.deviceAddress"
          placeholder="设备IP"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备端口" prop="port">
        <el-input
          v-model.number="dataForm.port"
          placeholder="设备端口"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select
          style="width:100%"
          v-model="dataForm.deviceType"
          placeholder="请选择设备类型"
        >
          <el-option
            v-for="item in optionsDeviceType"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="显示方向" prop="direction">
        <el-select
          style="width:100%"
          v-model="dataForm.direction"
          placeholder="请选择显示方向"
        >
          <el-option
            v-for="item in directionList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
        <el-select
          style="width:100%"
          v-model="dataForm.status"
          placeholder="请选择设备状态"
        >
          <el-option
            v-for="item in optionsStatus"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备作用" prop="virtue">
        <el-select
          style="width:100%"
          v-model="dataForm.virtue"
          placeholder="请选择设备状态"
        >
          <el-option label="车牌摄像头" :value="0" />
          <el-option label="车斗摄像头" :value="1" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="视频地址" prop="playUrl">
        <el-input v-model="dataForm.playUrl" placeholder="视频地址"></el-input>
      </el-form-item>
      <el-form-item label="云平台控制地址" prop="operateUrl">
        <el-input v-model="dataForm.operateUrl" placeholder="云平台控制地址"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isIp, isMobile } from "@/utils/validate";
export default {
  data() {
    var validateIsIp = (rule, value, callback) => {
      if (!isIp(value)) {
        callback(new Error("IP格式错误"));
      } else {
        callback();
      }
    };
    var validateIsPort = (rule, value, callback) => {
      if (value < 1 || value > 65535) {
        callback(new Error("端口格式错误"));
      } else {
        callback();
      }
    };
    return {
      optionsStatus: [],
      optionsDeviceType: [],
      optionsprojectId: [],
      optionspersonnelName: [],
      optionsSite: [],
      visible: false,
      dataForm: {
        id: 0,
        projectId: "",
        deviceSn: "",
        name: "",
        direction: null,
        deviceType: "",
        status: null,
        virtue: null,
        deviceAddress: "",
        port: null,
        username: "",
        password: ""
      },
      directionList: [
        {
          label: "驶入除渣场",
          value: 0
        },
        {
          label: "驶出除渣场",
          value: 1
        },
        {
          label: "驶入消纳场",
          value: 2
        },
        {
          label: "驶出消纳场",
          value: 3
        }
      ],
      dataRule: {
        projectId: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        deviceSn: [
          { required: true, message: "设备序列号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" }
        ],
        direction: [
          { required: true, message: "显示方向不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "设备状态不能为空", trigger: "blur" }
        ],
        deviceAddress: [
          { required: true, message: "IP不能为空", trigger: "blur" },
          { validator: validateIsIp, trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口不能为空", trigger: "blur" },
          { validator: validateIsPort, trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    changeCode(e) {
      //获取人员
      this.$http({
        url: this.$http.adornUrl(
          `/sys/getParamenters/getPersonnelProfile?projectCode=${e}`
        ),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionspersonnelName = data.list;
        }
      });
    },
    // 获取项目
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/project/management/list`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsprojectId = data.list;
        }
      });
      //获取设备类型
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getVideoDeviceType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsDeviceType = data.list;
        }
      });
      //获取设备状态
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getVideoDeviceStatus`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsStatus = data.list;
        }
      });
    },
    init(id) {
      this.getCompanyTypes();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bvideodevice/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectId = data.bVideoDevice.projectId;
              this.dataForm.deviceSn = data.bVideoDevice.deviceSn;
              this.dataForm.name = data.bVideoDevice.name;
              this.dataForm.deviceType = data.bVideoDevice.deviceType;
              this.dataForm.deviceAddress = data.bVideoDevice.deviceAddress;
              this.dataForm.direction = data.bVideoDevice.direction;
              this.dataForm.siteId = data.bVideoDevice.siteId;
              this.dataForm.status = data.bVideoDevice.status;
              this.dataForm.virtue = data.bVideoDevice.virtue;
              this.dataForm.deviceAddress = data.bVideoDevice.deviceAddress;
              this.dataForm.port = data.bVideoDevice.port;
              this.dataForm.username = data.bVideoDevice.username;
              this.dataForm.password = data.bVideoDevice.password;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bvideodevice/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              projectId: this.dataForm.projectId,
              deviceSn: this.dataForm.deviceSn,
              name: this.dataForm.name,
              deviceType: this.dataForm.deviceType,
              siteId: this.dataForm.siteId,
              direction: this.dataForm.direction,
              status: this.dataForm.status,
              virtue: this.dataForm.virtue,
              deviceAddress: this.dataForm.deviceAddress,
              port: this.dataForm.port,
              username: this.dataForm.username,
              password: this.dataForm.password
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
